#define  _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;
#include <assert.h>

namespace lw
{
	template<class T>
	struct ListNode
	{
		ListNode<T>* _next;
		ListNode<T>* _prev;
		T _val;
		ListNode(const T& val = T())
			:_next(nullptr)
			,_prev(nullptr)
			,_val(val)
		{}
	};

	template<class T, class Ref, class Ptr>
	struct __list_iterator
	{
		typedef ListNode<T> Node;
		typedef __list_iterator<T, Ref, Ptr> self;
		Node* _node;
		__list_iterator(Node* node)
			:_node(node)
		{}
		self& operator++()
		{
			_node = _node->_next;
			return *this;
		}
		self operator++(int)
		{
			self tmp(*this);
			_node = _node->_next;
			return tmp;
		}
		self& operator--()
		{
			_node = _node->_prev;
			return *this;
		}
		self operator--(int)
		{
			self tmp(*this);
			_node = _node->_prev;
			return tmp;
		}
		Ref operator*()
		{
			return _node->_val;
		}
		Ptr operator->()
		{
			return &(operator*());
		}
		bool operator!=(const self& s) const
		{
			return _node != s._node;
		}
	};

	template<class T>
	class list
	{
		typedef ListNode<T> Node;
	public:
		typedef __list_iterator<T, T&, T*> iterator;
		typedef __list_iterator<T, const T&, const T*> const_iterator;
		iterator begin()
		{
			return _head->_next;
		}
		iterator end()
		{
			return _head;
		}
		const_iterator begin() const
		{
			return _head->_next;
		}
		const_iterator end() const
		{
			return _head;
		}
		list()
		{
			_head = new Node();
			_head->_next = _head;
			_head->_prev = _head;
		}
		
		list(list<T>& lt)
		{
			_head = new Node();
			_head->_next = _head;
			_head->_prev = _head;

			for (const auto& e : lt)
				push_back(e);
		}

		void swap(list<T>& lt)
		{
			::swap(_head, lt._head);
		}

		list<T>& operator=(list<T> lt)
		{
			swap(lt);
			return *this;
		}

		~list()
		{
			clear();
			delete _head;
			_head = nullptr;
		}

		void clear()
		{
			iterator it = begin();
			while (it != end())
				it = erase(it);
		}

		iterator insert(iterator pos, const T& val)
		{
			Node* curr = pos._node;
			Node* prev = curr->_prev;
			Node* newnode = new Node(val);
			prev->_next = newnode;
			newnode->_prev = prev;
			newnode->_next = curr;
			curr->_prev = newnode;
			return newnode;
		}
		iterator erase(iterator pos)
		{
			assert(pos != end());
			Node* curr = pos._node;
			Node* prev = curr->_prev;
			Node* next = curr->_next;
			prev->_next = next;
			next->_prev = prev;
			delete curr;
			return next;
		}
		
		void push_back(const T& val)
		{
			insert(end(), val);
		}
		void push_front(const T& val)
		{
			insert(begin(), val);
		}
		void pop_back()
		{
			erase(--end());
		}
		void pop_front()
		{
			erase(begin());
		}

	private:
		Node* _head;
	};

	void Test1()
	{
		list<int> lt;
		lt.push_back(1);
		lt.push_back(2);
		lt.push_back(3);
		lt.push_back(4);
		lt.push_front(100);
		for (auto e : lt)
			cout << e << ' ';
		cout << endl;
		lt.insert(++lt.begin(), 200);

		for (auto e : lt)
			cout << e << ' ';
		cout << endl;

		lt.pop_back();
		lt.pop_front();
		for (auto e : lt)
			cout << e << ' ';
		cout << endl;

	}

	void Test2()
	{
		list<int> lt;
		lt.push_back(1);
		lt.push_back(2);
		lt.push_back(3);
		lt.push_back(4);
		lt.push_back(5);

		list<int> lt2 = lt;
		lt2.clear();
		lt2.push_back(100);
		lt2.push_back(200);
		lt2.push_back(300);
		lt = lt2;
		for (auto e : lt)
			cout << e << ' ';
		cout << endl;
	}
}

int main()
{
	lw::Test2();
	return 0;
}